package com.zhang.passer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.passer.domain.ImageDo;
import com.zhang.passer.domain.JiuDianDo;
import com.zhang.passer.mapper.ImageMapper;
import com.zhang.passer.mapper.JiuDianMapper;
import com.zhang.passer.service.JiudianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("jiudian")
public class
JiudianServiceImpl implements JiudianService {

    @Autowired
    private JiuDianMapper jiuDianMapper;

    @Autowired
    ImageMapper imageMapper;

    @Override
    public List<JiuDianDo> queryAll(JiuDianDo JiuDianDo) {
        QueryWrapper<JiuDianDo> JiuDianDoQueryWrapper = new QueryWrapper<>(JiuDianDo);
        return jiuDianMapper.selectList(JiuDianDoQueryWrapper);
    }


    @Override
    public Page<JiuDianDo> selectPage(int currentPage, int pageSize, QueryWrapper<JiuDianDo> queryWrapper) {
        //封装pageBean(Mybatis_plus中简单的分页模型)
        Page<JiuDianDo> page = new Page<>(currentPage,pageSize,true);
        //selectPage直接将结果返回给Page对象
        jiuDianMapper.selectPage(page, queryWrapper);
        //遍历所有景点填充景点图片列表
        List<JiuDianDo> records = page.getRecords();
        for (JiuDianDo record : records) {
            Map<String, Object> map = new HashMap<>();
            map.put("hid",record.getHid());
            record.setImages(imageMapper.selectByMap(map));
        }

        return page;
    }

    @Override
    public JiuDianDo queryDetail(int id) {

        JiuDianDo jiuDianDo = jiuDianMapper.selectOne(new QueryWrapper<>(new JiuDianDo().setHid(id)));
        jiuDianDo.setImages(imageMapper.selectList(new QueryWrapper<>(new ImageDo().setHid(id))));
        return jiuDianDo;
    }


}
